/**
 * Created by Administrator on 2017/2/25.
 */

var movieDetailDao = require('../dao/MovieDetailDao');
var movieCollectionDao = require('../dao/MovieCollectionDao');
var memberDao = require('../dao/MemberDao');
const model = require('../models/CommentModel');
var tool = require('../../common/utils/appUtils');

var async = require('async');

var cdn = 'http://cdn.bestmovie.com.cn/MOVIE/';
var poster = '/POSTER/';

var resultData = {};
module.exports = {
    /**
     * 影吧片库 --> 影片详情
     * @param req
     * @param callback
     * http -- get :framework7 a标签
     */
    getMovieDetailInfoModel: function (req, callback) {

        // var mid ='BESTV150625144819000001'
        // var open_id ='oSx2ps7hAjniYA1eKD5c5YdCLBco';



        var mid =''
        if(req.query.mid){
            mid = req.query.mid;
        }
        var open_id ='0';
        if(req.signedCookies.openid||req.signedCookies.open_id){
            open_id = req.signedCookies.openid||req.signedCookies.open_id;
        };
        async.parallel({
            movie: function (callBack) {
                movieDetailDao.getMovieInfo(mid, function (data) {
                    if (data.isSuccess) {
                        if (data.rows.length > 0) {
                            var movieInfo = [];
                            data.rows.forEach(function (val) {
                                var filmTime = (Math.floor(val.filmlength / 60) + '小时') + (val.filmlength % 60 + '分');
                                var movie_bigpic;
                                if(val.movie_bigpic){
                                    movie_bigpic = cdn + val.mid + poster + val.movie_bigpic;
                                }
                                movieInfo.push({
                                    mid: val.mid,
                                    movie_name: val.movie_name,
                                    filmlength: filmTime,
                                    score: val.score,
                                    category: val.category,
                                    publishdate: val.publishdate.format('yyyy-mm-dd'),
                                    director: val.director,
                                    country: val.country,
                                    cast: val.cast,
                                    brief: val.brief,
                                    poster: cdn + val.mid + poster + val.poster,
                                    movie_bigpic: movie_bigpic
                                });
                                callBack(null, movieInfo[0]);
                            });
                        }
                    } else {

                    }
                });
            },
            isCollection: function (callBack) {
                memberDao.selectByopenid(open_id, function (data) {
                    if (data.rows[0]) {
                        var member_id = data.rows[0].member_id;
                        console.log(member_id);
                        console.log(mid);
                        movieCollectionDao.selectMovieCollectionByMemberidAndMid([member_id, mid], function (data) {
                            if (data.isSuccess) {
                                if (data.rows[0]) {
                                    callBack(null, true);
                                } else {
                                    callBack(null, false);
                                }
                            } else {
                                callBack(null, false);
                            }
                        })
                    } else {
                        callBack(null, false);
                    }
                })
            },

            comments:function (callBack) {
                model.postCommentListNew([mid,1,open_id],function (result) {
                    if(result.data&&result.data.hasOwnProperty('rows')){
                        callBack(null,result.data.rows)
                    }else{
                        callBack(null,null)
                    }
                })
            }


        }, function (err, result) {
            if (err) {
                resultData = {
                    layout: false,
                    message: 'error',
                    status: 'error'
                };
            } else {
                resultData = {
                    layout: false,
                    message: 'success',
                    status: 'success',
                    movie: result.movie,
                    isCollection: result.isCollection,
                    comments:result.comments
                    // openid:open_id
                };
            };
            callback(resultData);
        });
    }
};
